package com.sghj.utils;

import java.io.*;
import java.net.*;

/**
 * @author zk
 * @version 1.0
 * @date 2023-10-31 14:44
 * @description
 */
public class HttpUtils {
    public static String sendGETRequest(String url) throws IOException {
        HttpURLConnection connection = null;
        BufferedReader reader = null;
        try {
            // 创建 URL 对象
            URL targetUrl = new URL(url);

            // 打开连接
            connection = (HttpURLConnection) targetUrl.openConnection();

            // 设置请求方法为 GET
            connection.setRequestMethod("GET");

            // 设置连接超时时间
            connection.setConnectTimeout(10000);

            // 设置读取超时时间
            connection.setReadTimeout(10000);

            // 发送请求
            connection.connect();

            // 读取响应
            InputStream inputStream = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuilder response = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }

            return response.toString();
        } finally {
            // 关闭连接和输入流
            if (reader != null) {
                reader.close();
            }
            if (connection != null) {
                connection.disconnect();
            }
        }
    }

    public static String sendPOSTRequest(String url, String requestBody) throws IOException {
        HttpURLConnection connection = null;
        BufferedReader reader = null;

//        System.out.println("requestBody:  "+requestBody);
        try {
            // 创建 URL 对象
            URL targetUrl = new URL(url);

            // 打开连接
            connection = (HttpURLConnection) targetUrl.openConnection();

            // 设置请求方法为 POST
            connection.setRequestMethod("POST");

            // 设置连接超时时间
            connection.setConnectTimeout(5000);

            // 设置读取超时时间
            connection.setReadTimeout(5000);

            // 允许输出流，即允许向服务器发送数据
            connection.setDoOutput(true);

            // 设置请求头部
            connection.setRequestProperty("content-type", "application/json");

            // 获取输出流，并写入请求体数据
            OutputStream outputStream = connection.getOutputStream();
            outputStream.write(requestBody.getBytes());
            outputStream.flush();

            // 发送请求
            connection.connect();

            // 读取响应
            InputStream inputStream = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuilder response = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }

            return response.toString();
        }
        finally {
            // 关闭连接和输入流
            if (reader != null) {
                reader.close();
            }
            if (connection != null) {
                connection.disconnect();
            }
        }
    }

    public static String sendPOSTRequestNoBody(String url) throws IOException {
        HttpURLConnection connection = null;
        BufferedReader reader = null;

        try {
            // 创建 URL 对象
            URL targetUrl = new URL(url);

            // 打开连接
            connection = (HttpURLConnection) targetUrl.openConnection();

            // 设置请求方法为 GET
            connection.setRequestMethod("POST");

            // 设置连接超时时间
            connection.setConnectTimeout(12000);

            // 设置读取超时时间
            connection.setReadTimeout(12000);

            // 发送请求
            connection.connect();

            // 读取响应
            InputStream inputStream = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuilder response = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }

            return response.toString();
        } finally {
            // 关闭连接和输入流
            if (reader != null) {
                reader.close();
            }
            if (connection != null) {
                connection.disconnect();
            }
        }
    }


}